/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lolbuilder.calculation;

import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import lolbuilder.Champions.Impl.GenericChamp;
import lolbuilder.Champions.Interf.DummyChamp;
import lolbuilder.build.Build;
import lolbuilder.fights.FightResult;
import lolbuilder.fights.Versus;
import lolbuilder.mainClasses.BestBuildFromFights;
import lolbuilder.mainClasses.ChampionsFights;
import lolbuilder.mainClasses.ItemBuildGenerator;
import lolbuilder.params.Duo;
import lolbuilder.params.Parameters;
import lolbuilder.runes.RBS;
import lolbuilder.runes.Runes;

/**
 *
 * @author chocoboSan
 */
public class FightThread extends Thread {
    public Stack<Build> pool=new Stack<Build>();
    public boolean stop=false;
    
    
    @Override
    public void run() {
        float step=(float) 0.1;
                
        while (stop==false || !pool.isEmpty()) {
            if (pool.isEmpty()) {
                continue;
            }
            DummyChamp g1=null;
         try {
            g1 = Parameters.champUsedForAd.getClass().newInstance();
        } catch (InstantiationException ex) {
            Logger.getLogger(ItemBuildGenerator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ItemBuildGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        DummyChamp g2=null;
         try {
            g2 = Parameters.champUsedForAd.getClass().newInstance();
        } catch (InstantiationException ex) {
            Logger.getLogger(ItemBuildGenerator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ItemBuildGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
            Build toTest=pool.pop();
            Parameters.r.addAllBonus(g1);
            Parameters.mbc.applyMasteries(g1);
            toTest.applyThisBuild(g1);
            RBS ennemyRunes=new RBS();
            ennemyRunes.ad+=15;
            ennemyRunes.armr+=13;
            ennemyRunes.rm+=12;
            int toTestNote=(int) (BestBuildFromFights.builds.size());
            if (g1.ms<420 /*&& !g1.canResistBigBurst*/) {
                BestBuildFromFights.buildsWithNotes.push(new Duo<Build,Integer>(toTest,0));
                continue;
            }
            for (Build b2 : BestBuildFromFights.builds) {
                g1.setSpells();
                
                g2.resetStats();
                /*Versus v=new Versus(new Duo<Build,Build>(toTest,b2));
                FightResult whoWon=BestBuildFromFights.whoWon.get(v);
                if (whoWon!=null) {
                    if (whoWon.whowon==0) {
                        if (whoWon.fight.matchup.a.equals(toTest)) {
                            continue;
                        } else {
                            toTestNote--;
                        }
                    } else if (whoWon.whowon==1) {
                        if (whoWon.fight.matchup.b.equals(toTest)) {
                            continue;
                        } else {
                            toTestNote--;
                        }
                    }
                }*/
                
                ennemyRunes.addAllBonus(g2);
                Parameters.mbc.applyMasteries(g2);
                b2.applyThisBuild(g2);
                int curH1=g1.hp;
                int curH2=g2.hp;
                float i=0;
                
                while (curH1>0 && curH2>0 && i<5) {
                    float MgcDamage1=g1.calculateMyMagicDmgForThisNumberOfSecOnThisTarget(g2, step);
                    float MgcDamage2=g2.calculateMyMagicDmgForThisNumberOfSecOnThisTarget(g1, step);
                
                    float damage1=(g1).calculateMyDmgsForThisNumberOfSecOnThisTarget(g2,step);
                
                    float damage2=(g2).calculateMyDmgsForThisNumberOfSecOnThisTarget(g1,step);
                    float regenHp1PerStep=(((float)g1.regenhp)/5)*step;
                    float regenHp2PerStep=(((float)g2.regenhp)/5)*step;
                
                    
                    curH1=(int) (((float)curH1)-damage2-MgcDamage2);
                    curH1+=regenHp1PerStep;
                    if (g1.ls>0) curH1+=((damage1*g1.ls)/100);
                    if (g1.sv>0) curH1+=((MgcDamage1*g1.sv)/100);
                    g1.curHp=curH1;
                    
                    curH2=(int) (((float)curH2)-damage1-MgcDamage1);
                    curH2+=regenHp2PerStep;
                    if (g2.ls>0) curH2+=(damage2*g2.ls/100);
                    if (g2.sv>0) curH1+=((MgcDamage1*g2.sv)/100);
                    g2.curHp=curH2;
                    
                    i+=step;
                    g1.updateTick(step);
                    g2.updateTick(step);
                }
                /*if (curH1<curH2) {
                    BestBuildFromFights.whoWon.put(v, new FightResult(v, 0));
                } else {
                    BestBuildFromFights.whoWon.put(v, new FightResult(v, 1));
                }*/
                if (curH1<curH2) {
                    toTestNote--;
                    if (toTestNote<BestBuildFromFights.bestNote) {
                        break;
                    }
                }
            }
            BestBuildFromFights.buildsWithNotes.push(new Duo<Build,Integer>(toTest,toTestNote));
        }
        synchronized(BestBuildFromFights.finished) {
            BestBuildFromFights.finished++;
        }
    }
}
